using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using CYZHAPI.Common.DB;
using CYZHAPI.Entity;
using CYZHAPI.IRepository;
using Oracle.ManagedDataAccess.Client;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace CYZHAPI.Repository
{
    public class ResterResultRep : ReponsitoryBase<RasterResultsModel>, IRasterResultRep
    {
        public ResterResultRep(IDapperContext dbContext) : base(dbContext) { }

        List<RasterResultsModel> IRasterResultRep.GetList(string sWhere)
        {
            List<RasterResultsModel> reList = new List<RasterResultsModel>();
            DataSet ds = base.GetList("RasterRESULTS", sWhere);
            if (ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    List<StaticModel> slist=new List<StaticModel>();
                    var stxt = dr["statictext"].ToString();
                    if(!String.IsNullOrEmpty(stxt)){
                        JArray array = (JArray)JsonConvert.DeserializeObject(stxt);
                    foreach (var jObject in array)
                    {
                        StaticModel sm=new StaticModel();
                        sm.code=jObject["code"].ToString();
                        sm.min=jObject["min"].ToString();
                        sm.max=jObject["max"].ToString();
                        sm.avg=jObject["avg"].ToString();
                        sm.dev=jObject["dev"].ToString();

                        slist.Add(sm);
                    }
                    
                    }
                    
                    RasterResultsModel objBean = new RasterResultsModel()
                    {
                        id = Convert.ToInt32(dr["id"]),
                        layername = dr["layername"].ToString(),
                        scope = dr["scope"].ToString(),
                        filetime = dr["filetime"].ToString(),
                        days = dr["days"] == DBNull.Value ? 0 : Convert.ToInt32(dr["days"]),
                        year = Convert.ToInt32(dr["year"]),
                        mouth = dr["mouth"] == DBNull.Value ? 0 : Convert.ToInt32(dr["mouth"]),
                        xun = dr["xun"] == DBNull.Value ? 0 : Convert.ToInt32(dr["xun"]),
                        filename = dr["filename"].ToString(),
                        algorithm = dr["algorithm"].ToString(),
                        createtime = Convert.ToDateTime(dr["createtime"]),
                        createby = dr["createby"].ToString(),
                        updatetime = Convert.ToDateTime(dr["updatetime"]),
                        updateby = dr["updateby"].ToString(),
                        timetype = dr["timetype"].ToString(),
                        staticText = slist,


                    };
                    reList.Add(objBean);
                    //foreach (DataColumn dc in ds.Tables[0].Columns) {}
                }
            }
            else
            {
                return null;
            }

            return reList;
        }
         bool IRasterResultRep.Delete (long key) {
            return base.delete (key, "id", "RasterRESULTS");
        }

    }
}